package com.kyzh.core.activities

import android.os.Bundle
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.base.fragment.BaseFragment
import com.gushenge.core.beans.Deal
import com.gushenge.core.dao.GlobalKeys
import com.kyzh.core.R
import com.kyzh.core.pager.weal.moneycard.FragmentMoneyCard
import com.kyzh.core.pager.weal.zhuanjin.GamePlayFragment
import com.kyzh.core.pager.gamelist.SortFragment
import com.kyzh.core.pager.me.settings.SettleFragment
import com.kyzh.core.pager.me.user.UnBindFragment
import com.kyzh.core.pager.weal.accountbuy.DealDynamicFragment
import com.kyzh.core.pager.weal.accountbuy.DealProductDetailFragment
import com.kyzh.core.pager.weal.accountbuy.MyDealFragment
import com.kyzh.core.pager.weal.accountbuy.RetrievePasswordFragment

class BaseFragmentActivity : BaseActivity() {

    private lateinit var fragment: BaseFragment
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_deal)
        val manager = supportFragmentManager
        val fragmentTransaction = manager.beginTransaction()
        fragment = getFragment()
        fragmentTransaction.add(R.id.frame, fragment).commit()

    }

    private fun getFragment(): BaseFragment {
        return when (intent.getIntExtra(GlobalKeys.TYPE, SOFT)) {
            SOFT -> SortFragment()
            DEAL_DYNAMIC -> DealDynamicFragment()
            MY_DEAL -> MyDealFragment()
            SETTLE -> {
                val deal = intent.getSerializableExtra(GlobalKeys.KEY) as Deal
                SettleFragment(deal)
            }
            DEAL_PRODUCT_DETAIL -> {
                val id = intent.getStringExtra(GlobalKeys.KEY)?:""
                DealProductDetailFragment(id)
            }
            UNBIND_PHONE -> {
                val desc = intent.getStringExtra(GlobalKeys.KEY)?:""
                UnBindFragment( true)
            }
            UNBIND_EMAIL -> {
                val desc = intent.getStringExtra(GlobalKeys.KEY)?:""
                UnBindFragment( false)
            }
            RETRIEVE_PASSWORD -> {
                RetrievePasswordFragment()
            }
            MONEYCARD -> {
                FragmentMoneyCard()
            }
            RENWUZHUANJIN ->{
                GamePlayFragment()
            }
            else -> SortFragment()
        }
    }

    companion object {
        const val SOFT = 1
        const val DEAL_DYNAMIC = 5
        const val MY_DEAL = 6
        const val SETTLE = 8
        const val DEAL_PRODUCT_DETAIL = 9
        const val UNBIND_PHONE = 10
        const val UNBIND_EMAIL = 11
        const val RETRIEVE_PASSWORD = 12
        const val MONEYCARD = 337
        const val RENWUZHUANJIN = 320
    }


    override fun onRestart() {
        super.onRestart()
        (fragment as? SettleFragment)?.selectOrder()
    }
}
